import shoppingFn from './shop' //拿到方法

let state = {
    productsArr:[],
}

let mutations = {
    setProducts(state){
        shoppingFn.getProducts((data)=>{
            state.productsArr=data //给数组赋值货品
        })
    },
    decInventory(state,val){
        state.productsArr.map(v=>{ //减少商品
            if(v.id==val.id){
                v.inventory--
            }
        })

    }
}

let actions = {
    getData({commit}){
        commit('setProducts')
    },
    reduceInventory({commit},val){
        commit('decInventory',val)
    }
}

let getters = {}

let shopping = {
    namespaced: true,
    state,
    mutations,
    actions,
    getters,
}

export default shopping